using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Ventigrate.Shared.ExternalMembership.Internal;

namespace Ventigrate.Shared.ExternalMembership.WebControls
{
    /// <summary>
    /// Code behind for UserDelete.aspx
    /// </summary>
    public class UserDelete : LayoutsPageBase
    {
        protected Label deleteMsg;

        protected override bool RequireSiteAdministrator
        {
            get { return true; }
        }

        protected override void OnLoad(EventArgs e)
        {
            // init
            this.CheckRights();

            // display error confirmation message
            string userName = Request.QueryString["USERNAME"];
            if (!string.IsNullOrEmpty(userName))
            {
                deleteMsg.Text = "Confirm that you want to delete the user '" + Request.QueryString["USERNAME"] + "'";
            }
            else
            {
                SPUtility.TransferToErrorPage("User Not Found");
            }
        }

        protected void OnDelete(object sender, EventArgs e)
        {
            string userName = Request.QueryString["USERNAME"];

            try
            {
                // delete user from FBA data store
                Membership.DeleteUser(userName);
            }
            catch (Exception ex)
            {
                ULS.Log.Error(ex);
                SPUtility.TransferToErrorPage(ex.Message);                 
            }
                        
            Response.Redirect("UsersDisp.aspx", false);
        }

    }
}
